package com.aiju.cash.ui.account.manager;

import android.text.TextUtils;

import com.aiju.app.DataProviderManager;
import com.aiju.cash.R;
import com.aiju.cash.util.GsonHelper;
import com.aiju.utils.PreferencesUtils;
import com.aiju.utils.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-03-30 10:27
 * @version: V1.0
 * @Description: 管理类目
 */
public class AccountTypeManager {

    private static String key = "accountType";

    public static void saveAccountType(List<AccountType> types) {
        String json = GsonHelper.object2JsonStr(types);
        PreferencesUtils.putString(DataProviderManager.getContext(), key, json);
    }

    /**
     * 获取全部类目
     *
     * @return
     */
    public static List<AccountType> getAccountType() {
        String accountType = PreferencesUtils.getString(DataProviderManager.getContext(), key);
        if (!TextUtils.isEmpty(accountType)) {
            return GsonHelper.convertEntities(accountType, AccountType.class);
        }
        return null;
    }

    private static List<AccountType> accountType;

    public static int getPic(String name) {
        if (Util.isEmpty(accountType)) {
            accountType = getAccountType();
        }
        for (AccountType t : accountType) {
            if (t.getName().equals(name)) {
                return t.getPic();
            }
        }
        return -1;
    }

    /**
     * 获取选中的类目
     *
     * @return
     */
    public static List<AccountType> getSelectAccountType() {
        String accountType = PreferencesUtils.getString(DataProviderManager.getContext(), key);
        if (!TextUtils.isEmpty(accountType)) {
            List<AccountType> select = new ArrayList<>();
            List<AccountType> accountTypes = GsonHelper.convertEntities(accountType, AccountType.class);
            for (AccountType type : accountTypes) {
                if (type.isSelect()) {
                    select.add(type);
                }
            }
            select.add(getAddAccountType());
            return select;
        }
        return null;
    }

    /**
     * 获取添加的类目
     *
     * @return
     */
    public static AccountType getAddAccountType() {
        return new AccountType("添加", R.mipmap.ic_account_type20, 19);
    }

    /**
     * 只在引导页初始化
     */
    public static void init() {
        List<AccountType> types = new ArrayList();
        types.add(new AccountType("通用", R.mipmap.ic_account_type1, 0));
        types.add(new AccountType("办公", R.mipmap.ic_account_type2, 1));
        types.add(new AccountType("采购", R.mipmap.ic_account_type3, 2));
        types.add(new AccountType("餐饮", R.mipmap.ic_account_type4, 3));
        types.add(new AccountType("房租", R.mipmap.ic_account_type5, 4));
        types.add(new AccountType("还款", R.mipmap.ic_account_type6, 5));
        types.add(new AccountType("进货", R.mipmap.ic_account_type7, 6));
        types.add(new AccountType("交通", R.mipmap.ic_account_type8, 7));
        types.add(new AccountType("员工", R.mipmap.ic_account_type9, 8));
        types.add(new AccountType("生活起居", R.mipmap.ic_account_type10, 9));
        types.add(new AccountType("水电", R.mipmap.ic_account_type11, 10));
        types.add(new AccountType("业务", R.mipmap.ic_account_type12, 11));
        types.add(new AccountType("医疗", R.mipmap.ic_account_type13, 12));
        types.add(new AccountType("营销", R.mipmap.ic_account_type14, 13));
        types.add(new AccountType("营业收入", R.mipmap.ic_account_type15, 14));
        types.add(new AccountType("住宿", R.mipmap.ic_account_type16, 15));
        types.add(new AccountType("转账", R.mipmap.ic_account_type17, 16));
        types.add(new AccountType("装修", R.mipmap.ic_account_type18, 17));
        types.add(new AccountType("其他", R.mipmap.ic_account_type19, 18));
        saveAccountType(types);
    }
}
